//
//  NSFileManager+Extension.m
//  OMTCoreKit
//
//  Created by 昭荣伊 on 2018/7/27.
//

#import "NSFileManager+Extension.h"

@implementation NSFileManager (Extension)

- (NSString *)omt_appPath {
    static NSString *_appPath;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSError * error = nil;
        NSArray *paths = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:NSHomeDirectory() error:&error];
        
        for ( NSString *path in paths) {
            if ([path hasSuffix:@".app"]) {
                _appPath = [NSString stringWithFormat:@"%@/%@", NSHomeDirectory(), path];
                break;
            }
        }
    });
    
    return _appPath;
}

- (NSString *)omt_docPath {
    static NSString *_docPath;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        _docPath = [paths objectAtIndex:0];
    });
    return _docPath;
}

- (NSString *)omt_libPrefPath {
    static NSString *_libPrefPath;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString * path = [[paths objectAtIndex:0] stringByAppendingFormat:@"/Preferences"];
        [self touch:path];
        _libPrefPath = path;
    });
    return _libPrefPath;
}

- (NSString *)omt_libCachePath {
    static NSString *_libCachePath;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString * path = [[paths objectAtIndex:0] stringByAppendingFormat:@"/Caches"];
        [self touch:path];
        _libCachePath = path;
    });
    return _libCachePath;
}

- (NSString *)omt_tmpPath {
    static NSString *_tmpPath;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString * path = [[paths objectAtIndex:0] stringByAppendingFormat:@"/tmp"];
        [self touch:path];
        _tmpPath = path;
    });
    return _tmpPath;
}

- (BOOL)touch:(NSString *)path {
    if ( NO == [[NSFileManager defaultManager] fileExistsAtPath:path] )
    {
        return [[NSFileManager defaultManager] createDirectoryAtPath:path
                                         withIntermediateDirectories:YES
                                                          attributes:nil
                                                               error:NULL];
    }
    
    return YES;
}

- (BOOL)touchFile:(NSString *)fileName {
    if ( NO == [[NSFileManager defaultManager] fileExistsAtPath:fileName] ) {
        return [[NSFileManager defaultManager] createFileAtPath:fileName
                                                       contents:[NSData data]
                                                     attributes:nil];
    }
    return YES;
}

@end
